package com.enjoyor.soa.traffic.restt.tpcms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tpcms.ApplyZoneDto;
import com.enjoyor.soa.traffic.api.dto.tpcms.InfoZoneDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.restt.tpcms.dubbo.IInfoZoneConsumeService;
import com.enjoyor.soa.traffic.util.helper.GeomtryHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Auth
@RestController
@RequestMapping({ "/infoZone" })
@Api("InfoZoneController页面请求")
@CrossOrigin
public class InfoZoneController {

    @Autowired
    private IInfoZoneConsumeService infoZoneConsumeService;

    /**
     * 获取区域列表信息
     * 
     * @param applyZoneId
     *            区域Id
     * @param applyZoneName
     *            区域名称
     * @param zoneTypeId
     *            区域类型Id
     * @param isUse
     *            开启状态(0为否，1为是；默认为1)
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    @GetMapping(value = { "/getInfoZoneList" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "获取申请区域列表数据", notes = "查询数据库中符合条件的操作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyZoneId", value = "applyZoneId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "applyZoneName", value = "applyZoneName", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "zoneTypeId", value = "zoneTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "isUse", value = "isUse", paramType = "query", dataType = "string") })
    public ResultPojo getInfoZoneList(@RequestParam(defaultValue = "", required = false) String applyZoneId,
            @RequestParam(defaultValue = "") String applyZoneName, @RequestParam(defaultValue = "") String zoneTypeId,
            @RequestParam(defaultValue = "") String isUse, HttpServletRequest request) {
        List<ApplyZoneDto> infoZoneList = (List<ApplyZoneDto>) infoZoneConsumeService
                .getInfoZoneList(applyZoneId, applyZoneName, zoneTypeId, isUse).getResultList();
        for (ApplyZoneDto applyZoneDto : infoZoneList) {
            if (applyZoneDto.getZoneGeo() != null) {
                String geom2JsonString = GeomtryHelper.getGeom2JsonString(applyZoneDto.getZoneGeo());
                applyZoneDto.setZoneGeoString(geom2JsonString);
            }
        }
        return new ResultPojo(infoZoneList);//ResultHelper.getResultJson(infoZoneList).toJSONString();
    }

    /**
     * 获取区域分页信息
     * 
     * @param page
     * @param request
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @GetMapping(value = { "/getInfoZonePage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyZoneId", value = "applyZoneId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "applyZoneName", value = "applyZoneName", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "zoneTypeId", value = "zoneTypeId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "isUse", value = "isUse", paramType = "query", dataType = "string") })
    public ResultPojo getInfoZonePage(@RequestParam(defaultValue = "", required = false) String applyZoneId,
            @RequestParam(defaultValue = "") String applyZoneName, @RequestParam(defaultValue = "") String zoneTypeId,
            @RequestParam(defaultValue = "") String isUse, Page page, HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("applyZoneId", applyZoneId);
        map.put("applyZoneName", applyZoneName);
        map.put("zoneTypeId", zoneTypeId);
        map.put("isUse", isUse);
        page.setMapBean(map);
        //String result = ResultHelper.getResultJson(infoZoneConsumeService.getInfoZonePage(page)).toJSONString();
        return infoZoneConsumeService.getInfoZonePage(page);
    }

    /**
     * 新增申请区域信息
     * 
     * @param applyZoneDto
     * @param request
     * @return
     */
    @PostMapping(value = { "/insertInfoZone" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("新建区域信息")
    public ResultPojo insertInfoZone(@RequestBody ApplyZoneDto applyZoneDto, HttpServletRequest request) {
        return infoZoneConsumeService.insertInfoZone(applyZoneDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 修改区域信息
     * 
     * @param applyZoneDto
     * @param request
     * @return
     */
    @PutMapping(value = { "/editInfoZone" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("更新数据")
    public ResultPojo editInfoZone(@RequestBody ApplyZoneDto applyZoneDto, HttpServletRequest request) {
        return infoZoneConsumeService.editInfoZone(applyZoneDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 删除区域信息
     * 
     * @param applyZoneId
     * @param request
     * @return
     */
    @DeleteMapping(value = { "/deleteInfoZone" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除数据")
    @ApiImplicitParam(name = "applyZoneId", value = "applyZoneId", paramType = "query", dataType = "string")
    public ResultPojo deleteInfoZone(@RequestParam(defaultValue = "") String applyZoneId, HttpServletRequest request) {
        return infoZoneConsumeService.deleteInfoZone(applyZoneId,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    /**
     * 绘制区域信息
     * 
     * @param area
     *            区域信息dubbo坐标数据集
     * @param applyZoneId
     *            区域Id
     * @param request
     * @return
     */
    @PutMapping(value = { "/drawingZoneShape" }, consumes = { "application/x-www-form-urlencoded" })
    @ResponseBody
    @ApiOperation("绘制地图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyZoneId", value = "applyZoneId", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "geom", value = "geom", paramType = "query", dataType = "string") })
    public ResultPojo drawingZoneShape(@RequestParam(required = true, value = "geom") String geom,
            @RequestParam(required = true, value = "applyZoneId") String applyZoneId, HttpServletRequest request) {
        InfoZoneDto infoZone = new InfoZoneDto();
        infoZone.setApplyZoneId(applyZoneId);
        return infoZoneConsumeService.drawingZoneShape(infoZone, geom,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

}
